<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// autor/i etapy: 
// 29.11.2013

class Admin extends CI_Controller {
   
  //presmeruje na login
	public function index(){
		 $this->admin_view();
	}
  
  //zobrazi hlavny view pre admina ak je login a admin, inac restricted
  public function admin_view(){
    if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
		$this->load->model('model_main');
		$data['text']=$this->model_main->get_text('welcome_text');
        $this->template->make_template($data, 'admin_main');
    } else
        redirect('main/restricted');
  }
  
  //zobrazi rozvrh pre admina
  public function rozvrh(){
    if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
        $this->template->make_template("", 'admin_rozvrh');
    } else
        redirect('main/restricted');
  }
  
  //zobrazi view pridajUdalost ak je login
  public function addUdalost(){
    if ($this->session->userdata('is_logged_in')){
        $this->load->model('model_admin');  
        //$data['field'] = $this->model_admin->get_zamerania();
        /*$data['test'] = array(
                   '0'  => 'Matematika',
                   '1'  => 'Fyzika',
                   '2'  => 'Informatika',
                   '3'  => 'Iné'
                 ); */
        $data['udal'] = $this->model_admin->get_zamerania();         
   //     print_r($data['udal']);
		$this->template->make_template($data, 'admin_pridajUdalost');
    } else{
        redirect('main/restricted');
    }
  } 
  
  //nacita view na pridanie terminu
  public function addTermin(){
      if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
          $this->load->model('model_admin');
          $data['termin'] = $this->model_admin->get_udalosti();     
          $this->template->make_template($data, 'admin_pridajTerminUdalosti');
      }  else{
          redirect('main/restricted');
      }  
  }
  //zobrazi view rozvrhAdmin ak je login
  public function rozvrhAdmin(){
    if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
        $this->template->make_template("",'admin_rozvrh');
    } else{
        redirect('main/restricted');
    }
  }
  
  //skontroluje ci su udaje pre novu udaost validne, ak ano, vykona sa funkcia addUdalost
  public function udalost_validation(){
    if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
      $this->load->library('form_validation');
      $this->form_validation->set_rules('nazov','Nazov','required|trim|is_unique[udalost.nazov]');
      $this->form_validation->set_rules('miestnost','Miestnost','required|trim');
      $this->form_validation->set_rules('abstrakt','Abstrakt','required|trim');
      $this->form_validation->set_rules('ucitel','Ucitel','required|trim');
      $this->form_validation->set_rules('maxLud','MaxLud','integer|required|trim');
      $this->form_validation->set_rules('maxprednasok','MaxPrednasok','integer|required|trim');
      //kontrola presla
      if ($this->form_validation->run()){
          $this->load->model('model_admin');
          if ($zameranie = NULL) redirect('admin/addUdalost'); 
          if ($this->model_admin->addUdalost()){
            $this->template->make_template("",'admin_main');
          } else redirect('admin/addUdalost');
      } else{
          redirect('admin/addUdalost'); 
      }  
    } else redirect('main/restricted');
  }
 
  // skontroluje ci su udaje pre termin validne 
  //trebalo komplikvoanajsie prehodit. z postu vracia index pola, nie nazov, preto treba nazov, a z neho id
  public function termin_validation(){
    if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
      $this->load->library('form_validation');
      $this->form_validation->set_rules('udalost','Udalost','required|trim');
      $this->form_validation->set_rules('trvanie','Trvanie','integer|required|trim');
      if ($this->form_validation->run()){
          $this->load->model('model_admin');
          
          
          $this->load->model('model_rozvrh');
          $casOd = $this->model_rozvrh->StrToMins($this->input->post('casOd'));
          $casDo = $this->model_rozvrh->StrToMins($this->input->post('casDo'));
          $trvanie = $this->input->post('trvanie');
          if ($trvanie <= 29){
              //echo "boom";
              redirect('admin/zleUdaje');
          } 
          //echo $casOd; echo "<br>"; echo $casDo; echo "<br>"; echo $trvanie;

             if ($casOd + $trvanie +10 > $casDo){ 
              redirect('admin/zleUdaje');
 				     return;
          
          }
      
          $data['ud'] = $this->model_admin->get_udalosti();
          $temp = array();
          foreach($data['ud'] as $prvok){
              $temp[] =  $prvok;   
          }  
          $udalost = $this->model_admin->get_id_udalosti($temp[$this->input->post('udalost')]);
          if ($this->model_admin->addTermin($udalost)){
              redirect('rozvrh');   
          } else redirect('admin/addTermin');  
      } else{
          redirect('admin/zleUdaje');
      }
    } else redirect('main/restricted');
  }
  
  public function zleUdaje(){
      $this->template->make_template("",'admin_zleUdaje');
  } 
  
  //skontroluje ci su udaje pre zmenu udalosti validne, ak ano, zmeni
  public function udalost_zmenaValidation($key){
    if (($this->session->userdata('is_logged_in')) and ($this->session->userdata('admin'))){
        $this->load->library('form_validation');
		$this->load->model('model_admin');
		$udalost = $this->model_admin->get_info_udalosti($key);
		if($this->input->post('nazov') == $udalost['nazov']){
			$isunique = '';
		} else {
			$isunique = '|is_unique[udalost.nazov]';			
		}		
        $this->form_validation->set_rules('nazov','Nazov','required|trim'.$isunique);    
        $this->form_validation->set_rules('miestnost','Miestnost','required|trim');
        $this->form_validation->set_rules('abstrakt','Abstrakt','required|trim');
        $this->form_validation->set_rules('ucitel','Ucitel','required|trim');
        $this->form_validation->set_rules('maxLud','MaxLud','integer|required|trim');
        $this->form_validation->set_rules('maxprednasok','MaxPrednasok','integer|required|trim');
        //preslo
        if ($this->form_validation->run()){
            if($this->model_admin->zmenit_Udalost($key)){
				// Udalost sa zmenila, spat na rozvrh.
				redirect('rozvrh'); 
			} else {
				echo 'Error pri vkladani do DB';	
			}
        } else {
            /*$udalost['id_ud'] = $key;
            $data = array(
               'udalost' => $udalost
            );
            $this->template->make_template($data, 'admin_rozvrh_udalost'); */
            redirect('admin/zleUdaje');
        }
        
    } else{
        redirect('main/restricted');
    }
  }
  
  public function udalost_zmena_terminu_Validation($key){
    if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
      $this->load->library('form_validation');
      $this->form_validation->set_rules('trvanie','Trvanie','integer|required|trim');
      if ($this->form_validation->run()){
          $this->load->model('model_admin');
          
          
          $this->load->model('model_rozvrh');
          $casOd = $this->model_rozvrh->StrToMins($this->input->post('casOd'));
          $casDo = $this->model_rozvrh->StrToMins($this->input->post('casDo'));
          $trvanie = $this->input->post('trvanie');
          if ($trvanie <= 29){
              redirect('rozvrh');
          } 
          //echo $casOd; echo "<br>"; echo $casDo; echo "<br>"; echo $trvanie;

             if ($casOd + $trvanie +10 > $casDo){ 
               redirect('rozvrh'); 
			 	return;

          
          }
          if ($this->model_admin->zmena_casu_udalosti($key)){
              redirect('rozvrh'); 
          } else echo "nepreslo"; 
      } else{
          redirect('admin/zleUdaje');
      }
    } else redirect('main/restricted');
  }
  
   //vymaze cas ud. z DB
  public function cas_udalosti_delete($key){
      $this->load->library('email', array('mailtype'=>'html'));
      if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
          $this->load->model('model_admin');
		  if($this->model_admin->zmazat_cas_udalosti($key)){
				redirect('rozvrh');  
		  } else {
			  echo 'chyba - nepodarilo sa zmazat';
		  }
      }else{
        redirect('main/restricted');
      }
  }
  
  //vymaze udalost z DB
  public function udalost_delete($key){
	  $this->load->library('email', array('mailtype'=>'html'));
      if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
          $this->load->model('model_admin');
		  if($this->model_admin->zmazat_Udalost($key)){
				redirect('rozvrh');  
		  } else {
			  echo 'chyba - nepodarilo sa zmazat';
		  }
      }else{
        redirect('main/restricted');
      }
  }
  
  public function vypis_info($id){
      $this->load->model('model_user');
      $this->load->model('model_admin');
      $this->load->model('model_rozvrh');
      $kolektivsAll = array();
			$id_ud = $this->model_rozvrh->get_id_udalosti($id);
      $kapacita = $this->model_user->get_maxLudi($id_ud['id']);
      $zaplnenie = $this->model_user->get_zaplnenie($id);
      $data['kapacita'] = $kapacita;
      $data['zaplnenie'] = $zaplnenie;
      $kolektivy = $this->model_admin->get_prihlasene_kolektivy($id);
      foreach ($kolektivy as $row){
          $info_kolektiv = $this->model_user->get_info_kolektiv_forAdmin($row);
          $ucitel_id = $this->model_admin->get_ucitel_id($row);
          $info_kolektiv['ucitel'] = $this->model_admin->get_ucitel_info($ucitel_id);
          $kolektivsAll[] = $info_kolektiv;
          //echo $row; echo "<br>";
      }
      //print_r($kolektivsAll);
      $data['kolektivy'] = $kolektivsAll;
      $this->template->make_template($data,'admin_rozvrh_udalost_zaplnenie');
  }
  
  public function mail_zmena_validation(){
  if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
      $this->load->library('form_validation');
      $this->form_validation->set_rules('novy_email',"Nový email",'required|trim|valid_email');
      $this->form_validation->set_rules('confirm_email',"Podvrdiť email",'required|trim|matches[novy_email]');
      if ($this->form_validation->run()){
        $this->load->model('model_admin');
        $this->model_admin->zmena_emailu($this->session->userdata('email'), $this->input->post('novy_email'));
        $this->session->set_userdata('email', $this->input->post('novy_email'));
        $this->template->make_template("", 'admin_main');
     } else{
        $this->template->make_template("", 'admin_main');
     }
  }else{
        redirect('main/restricted');
  }
  }
  
  public function zmena_textu(){
  if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){

        $this->load->model('model_admin');
        $this->model_admin->zmena_textu('welcome_text', $this->input->post('text'));
   
         $this->admin_view();

  }else{
        redirect('main/restricted');
  }
  
}

  public function mazanie_dat(){
  if ($this->session->userdata('is_logged_in') and $this->session->userdata('admin')){
    
        $this->load->model('model_admin');
             if ($this->input->post("terminy_zmaz_submit") != NULL){
              $this->model_admin->zmaz_terminy();
          }
          else if ($this->input->post("udalosti_zmaz_submit") != NULL){
              $this->model_admin->zmaz_udalosti();
          } 
         $this->admin_view();
   
  }else{
        redirect('main/restricted');
  }
  
}


}